#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: 
@time: 2024/12/2 21:36
@Function: 注册校验
"""

from fastapi import APIRouter, status, HTTPException
from pydantic import BaseModel, Field
from orm.models import User_Register

api_register = APIRouter()


class user_register_info(BaseModel):
    username: str = Field(..., title="注册用户名", description="用户名")
    password: str = Field(..., title="注册密码", description="密码")


@api_register.post('/register', status_code=status.HTTP_200_OK)
async def user_register(user_register: user_register_info):
    register_username = await User_Register.filter(
        username=user_register.username).first()
    if register_username is not None:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
                            detail="用户名已注册，请重新输入")
    else:
        register_result = await User_Register.create(
            username=user_register.username, password=user_register.password)
        return {
            "status": status.HTTP_200_OK,
            "message": "注册成功",
            "data": {
                "username": register_result.username
            }
        }
